package net.edu_soft.ec.asset.controller;


import net.edu_soft.ec.asset.pojo.CloudMessage;
import net.edu_soft.ec.asset.pojo.MessageType;
import net.edu_soft.ec.asset.service.CloudMessageService;
import net.edu_soft.ec.asset.service.MessageTypeService;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/CloudMessage")
public class CloudMessageController {

    @Autowired
    private MessageTypeService messageTypeService;

    @Autowired
    private CloudMessageService cloudMessageService;


    //新增通知类型
    @RequestMapping(value = "/addMessageType")
    public ResultVo addCloudMessageType(MessageType messageType){
        if (messageTypeService.insert(messageType)!=1){
            return ResultVo.failed(201,"操作失败",null);
        }
        return ResultVo.succeed("ok",null);
    }

    //获取通知类型
    @RequestMapping(value = "/getMessageType")
    @RequiresPermissions("CloudMessage:getMessageType")
    public ResultVo getCloudMessageType(){
        return ResultVo.succeed("ok",messageTypeService.selectByExample());
    }


    //删除类型
    @RequestMapping(value = "/deleMessageType")
    public ResultVo deleMessageType(Integer id){
        if (messageTypeService.deleteByPrimaryKey(id)!=1){
            return ResultVo.failed(201,"操作失败",null);
        }
        return ResultVo.succeed("ok",null);
    }

    //创建新闻/通知
    @RequestMapping(value = "/addMessage")
    public ResultVo addMessage(CloudMessage cloudMessage, MultipartFile mainFile){
        System.out.println("要发布的消息类：类型："+cloudMessage.getMessageType()+"内容："+cloudMessage);
        if ( cloudMessageService.addCloudMessage(cloudMessage,mainFile)!=1){
            return ResultVo.failed(201,"操作失败",null);
        }
        return ResultVo.succeed("ok",null);
    }





    //获取新闻通知记录
    @RequestMapping(value = "/getMessage")
    public ResultVo getMessage(){
        List<CloudMessage> cloudMessages=cloudMessageService.getCloudMessageList();
        return ResultVo.succeed("ok",cloudMessages);
    }

    //编辑新闻/通知
    @RequestMapping(value = "/updateMessage")
    public ResultVo updateMessage(CloudMessage cloudMessage,MultipartFile newImg){
        if (cloudMessageService.updataCloudMessage(cloudMessage,newImg)!=1){
            return ResultVo.failed(201,"操作失败",null);
        }
        return ResultVo.succeed("ok",null);
    }



    //删除新闻/通知
    @RequestMapping(value = "/deletMessage")
    public ResultVo deletMessage(Integer id){
        if (cloudMessageService.deleCloudMessage(id)!=1){
            return ResultVo.failed(201,"操作失败",null);
        }
        return ResultVo.succeed("ok",null);
    }

}
